<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><script>var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?724387c702f2fe1e6416cc40649fa3a5";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}()</script><link rel="apple-touch-icon" sizes="180x180" href="/images/LOGO.png"><link rel="icon" type="image/png" sizes="32x32" href="/images/32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/images/16x16.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><link rel="alternate" href="/atom.xml" title="墨轩技术糖" type="application/atom+xml"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="baidu-site-verification" content="c2KFYiIkF1"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/npm/pace-js@1/themes/blue/pace-theme-minimal.css"><script src="//cdn.jsdelivr.net/npm/pace-js@1/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Gemini",version:"7.4.2",exturl:!1,sidebar:{position:"right",display:"hide",offset:12,onmobile:!1},copycode:{enable:"ture",show_result:!0,style:"mac"},back2top:{enable:!0,sidebar:!0,scrollpercent:"ture"},bookmark:{enable:!1,color:"#222",save:"auto"},fancybox:!1,mediumzoom:!1,lazyload:!1,pangu:!1,algolia:{appID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:5,unescape:!1,preload:!1},path:"search.xml",motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}},translation:{copy_button:"复制",copy_success:"复制成功",copy_failure:"复制失败"},sidebarPadding:40}</script><meta property="og:type" content="article"><meta property="og:title" content="nodejs笔记"><meta property="og:url" content="https://moxuan.xyz/2019/11/21/nodejs/index.html"><meta property="og:site_name" content="墨轩技术糖"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://moxuan.xyz/2019/11/21/nodejs/17s.jpg"><meta property="article:published_time" content="2019-11-21T12:39:33.000Z"><meta property="article:modified_time" content="2019-11-21T12:45:04.039Z"><meta property="article:author" content="墨轩啊"><meta property="article:tag" content="记录"><meta property="article:tag" content="Node"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://moxuan.xyz/2019/11/21/nodejs/17s.jpg"><link rel="canonical" href="https://moxuan.xyz/2019/11/21/nodejs/"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0,isPage:!1,isArchive:!1}</script><meta name="baidu-site-verification" content="c2KFYiIkF1"><meta name="google-site-verification" content="5kgxkt2DF9YLzuaYNnUWuPtH85t70Y672QgJwS22e6o"><title>nodejs笔记 | 墨轩技术糖</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-meta"><div><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">墨轩技术糖</span><span class="logo-line-after"><i></i></span></a></div><h1 class="site-subtitle" itemprop="description">心性无染，温文儒雅</h1></div><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span><span class="toggle-line toggle-line-middle"></span><span class="toggle-line toggle-line-last"></span></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-fw fa-home"></i> 首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i> 关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i> 标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i> 分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i> 归档</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i> 站点地图</a></li><li class="menu-item menu-item-建站日志"><a href="/%E5%BB%BA%E7%AB%99%E6%97%A5%E5%BF%97/" rel="section"><i class="fa fa-fw fa-home"></i> 建站日志</a></li><li class="menu-item menu-item-demo"><a href="/demo/" rel="section"><i class="fa fa-fw fa-home"></i> demo</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i> 搜索</a></li></ul></nav><div class="site-search"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocorrect="off" autocapitalize="none" placeholder="搜索..." spellcheck="false" type="text" id="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"></div></div><div class="search-pop-overlay"></div></div></div></header><div class="reading-progress-bar"></div><a href="https://github.com/moxuan-jst" class="github-corner" title="moxuan-jst GitHub" aria-label="moxuan-jst GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0 0 115 115 130 115 142 142 250 250 250 0Z"></path><path d="M128.3 109C113.8 99.7 119 89.6 119 89.6 122 82.7 120.5 78.6 120.5 78.6 119.2 72 123.4 76.3 123.4 76.3 127.3 80.9 125.5 87.3 125.5 87.3 122.9 97.6 130.6 101.9 134.4 103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115 115C114.9 115.1 118.7 116.5 119.8 115.4L133.7 101.6C136.9 99.2 139.9 98.4 142.2 98.6 133.8 88 127.5 74.4 143.8 58 148.5 53.4 154 51.2 159.7 51 160.3 49.4 163.2 43.6 171.4 40.1 171.4 40.1 176.1 42.5 178.8 56.2 183.1 58.6 187.2 61.8 190.9 65.4 194.5 69 197.7 73.2 200.1 77.6 213.8 80.2 216.3 84.9 216.3 84.9 212.7 93.1 206.9 96 205.4 96.6 205.1 102.4 203 107.8 198.3 112.5 181.9 128.9 168.3 122.5 157.7 114.1 157.9 116.9 156.7 120.9 152.7 124.9L141 136.5C139.8 137.7 141.6 141.9 141.8 141.8Z" fill="currentColor" class="octo-body"></path></svg></a><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content"><div class="posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="https://moxuan.xyz/2019/11/21/nodejs/"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/images/tlogo.png"><meta itemprop="name" content="墨轩啊"><meta itemprop="description" content="专注前端技术，包括vue,react，涉猎的主要编程语言是Python,JavaScript，Node，朝着全栈方向努力！。"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="墨轩技术糖"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">nodejs笔记</h2><div class="post-meta"><div style="margin-top:10px"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-keyboard-o"></i> <span title="post.wordcount">本篇博文共计 2.4k 字</span></span></span> <span class="post-time">&nbsp; | &nbsp;<span class="post-meta-item-icon"><i class="fa fa-hourglass-half"></i> <span title="post.min2read">阅读约 11 分钟</span></span></span></div><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i></span> <span class="post-meta-item-text">发表于</span> <time title="创建时间：2019-11-21 20:39:33 / 修改时间：20:45:04" itemprop="dateCreated datePublished" datetime="2019-11-21T20:39:33+08:00">2019-11-21</time></span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-folder-o"></i></span> <span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E8%AE%B0%E5%BD%95/" itemprop="url" rel="index"><span itemprop="name">记录</span></a></span> ， <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E8%AE%B0%E5%BD%95/Node/" itemprop="url" rel="index"><span itemprop="name">Node</span></a></span> ， <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/%E8%AE%B0%E5%BD%95/Node/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">前端</span></a></span></span><span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i></span> <span class="post-meta-item-text">阅读次数：</span><span id="busuanzi_value_page_pv"></span></span></div></header><div class="post-body" itemprop="articleBody"><p><img src="/2019/11/21/nodejs/17s.jpg" alt></p><a id="more"></a><h3 id="用途"><a href="#用途" class="headerlink" title="用途"></a>用途</h3><ul><li>中间层<ul><li>安全性</li><li>性能优势</li><li>降低主服务器的复杂度</li></ul></li><li>工具</li><li>一些小型服务</li></ul><h3 id="优势"><a href="#优势" class="headerlink" title="优势"></a>优势</h3><ul><li>便于前端入手</li><li>性能高</li><li>利于和前端代码整合</li></ul><h3 id="卸载"><a href="#卸载" class="headerlink" title="卸载"></a>卸载</h3><ul><li>应用程序中卸载，删除nodejs目录</li><li>手动删除安装目录的node_modules</li><li>手动删除用户目录下的node_modules</li></ul><h3 id="系统自带模块"><a href="#系统自带模块" class="headerlink" title="系统自带模块"></a>系统自带模块</h3><h4 id="http"><a href="#http" class="headerlink" title="http"></a><code>http</code></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> http = <span class="built_in">require</span>(<span class="string">"http"</span>);</span><br><span class="line"><span class="keyword">let</span> server = http.createServer( <span class="function">(<span class="params">request, response</span>) =&gt;</span> &#123;</span><br><span class="line">	<span class="built_in">console</span>.log(request, response);</span><br><span class="line">    <span class="comment">//require.url :客户端请求地址</span></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    	response.writeHead(404);  响应头</span></span><br><span class="line"><span class="comment">		response.write('Not Found'); 返回的响应体</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">&#125;);</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br></pre></td></tr></table></figure><h4 id="fs"><a href="#fs" class="headerlink" title="fs"></a><code>fs</code></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>);</span><br><span class="line"><span class="comment">// 写文件</span></span><br><span class="line"><span class="comment">// fs.writeFile(path, date, callback);</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">// 读文件</span></span><br><span class="line"><span class="comment">// fs.readFile(path, buffer);</span></span><br><span class="line"></span><br><span class="line">fs.writeFile(<span class="string">"./www/a.txt"</span>, <span class="string">"dadawadawda"</span>, <span class="function"><span class="keyword">function</span> (<span class="params">err</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(err)&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(err);</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">"成功"</span>);</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;) ;</span><br><span class="line">fs.readFile(<span class="string">"./www/a.txt"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">err, data</span>) </span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(err)&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(err);</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(data);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h4 id="querystring"><a href="#querystring" class="headerlink" title="querystring"></a><code>querystring</code></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//处理请求“xxx=xx&amp;xxx=xx”</span></span><br><span class="line"><span class="keyword">let</span> querystring = requir(<span class="string">"querystring"</span>);</span><br><span class="line"><span class="keyword">let</span> result = querystring.parse(req.url); </span><br><span class="line"><span class="keyword">let</span> result = querystring.stringify(&#123;<span class="attr">a</span>: <span class="number">12</span>, <span class="attr">b</span>: <span class="number">2</span>, <span class="attr">c</span>: <span class="string">"dss"</span>&#125;) <span class="comment">//a=12&amp;b=2&amp;c=dss</span></span><br></pre></td></tr></table></figure><h4 id="url"><a href="#url" class="headerlink" title="url"></a><code>url</code></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//处理GET请求"a.html?xxx=xx&amp;xxx=xx"</span></span><br><span class="line"><span class="keyword">let</span> url = <span class="built_in">require</span>(<span class="string">"url"</span>);</span><br><span class="line"><span class="keyword">let</span> result = url.prase(req.url, <span class="literal">true</span>);</span><br></pre></td></tr></table></figure><h4 id="assert-断言"><a href="#assert-断言" class="headerlink" title="assert 断言"></a><code>assert</code> 断言</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> assert = <span class="built_in">require</span>(<span class="string">"assert"</span>);</span><br><span class="line"></span><br><span class="line">assert( <span class="number">5</span>&gt;<span class="number">3</span>, <span class="string">"aaa"</span> );</span><br><span class="line"></span><br><span class="line">assert.deepEqual(变量, 预期值, msg)</span><br><span class="line">assert.deepStrictEqual(变量, 预期值, msg)</span><br></pre></td></tr></table></figure><h4 id="path"><a href="#path" class="headerlink" title="path"></a><code>path</code></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">path.dirname(str)  <span class="comment">//获取文件夹路径</span></span><br><span class="line">path.extname(str)  <span class="comment">//获取文件后缀名</span></span><br><span class="line">path.basename(str) <span class="comment">//获取文件名</span></span><br><span class="line">path.resolve(__dirname, str)  <span class="comment">//解析绝对路径</span></span><br></pre></td></tr></table></figure><h3 id="服务器相关内容"><a href="#服务器相关内容" class="headerlink" title="服务器相关内容"></a>服务器相关内容</h3><h4 id="http协议"><a href="#http协议" class="headerlink" title="http协议"></a>http协议</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">http 1.0:</span><br><span class="line">http 1.1: 持久链接</span><br><span class="line">https:   基于安全协议 </span><br><span class="line">http 2.0: 加密、头部压缩、服务器推送、多路复用</span><br><span class="line">http 3.0:</span><br></pre></td></tr></table></figure><h4 id="响应码"><a href="#响应码" class="headerlink" title="响应码"></a>响应码</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1xx: 信息</span><br><span class="line">2xx: 成功响应 200</span><br><span class="line">3xx: 重定向 301</span><br><span class="line">4xx: 失败响应 文件不存在 404</span><br><span class="line">5xx: 服务器错误 502</span><br></pre></td></tr></table></figure><h4 id="请求方式"><a href="#请求方式" class="headerlink" title="请求方式"></a>请求方式</h4><h6 id="GET处理"><a href="#GET处理" class="headerlink" title="GET处理"></a><code>GET</code>处理</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//处理GET请求"/a.html?xxx=xx&amp;xxx=xx"</span></span><br><span class="line"><span class="keyword">let</span> url = <span class="built_in">require</span>(<span class="string">"url"</span>);</span><br><span class="line"><span class="keyword">let</span> result = url.prase(req.url, <span class="literal">true</span>);</span><br></pre></td></tr></table></figure><h6 id="POST处理"><a href="#POST处理" class="headerlink" title="POST处理"></a><code>POST</code>处理</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">post发送的数据是分段的</span></span><br><span class="line"><span class="comment">req.on("data"); 数据开始接受的事件</span></span><br><span class="line"><span class="comment">req.on("end")； 数据接受结束后的事件</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">let</span> arr = [] <span class="comment">//接受buffer</span></span><br><span class="line">req.on(<span class="string">"data"</span>, <span class="function"><span class="keyword">function</span>(<span class="params">buffer</span>)</span>&#123;</span><br><span class="line">    arr.push(buffer);</span><br><span class="line">&#125;);</span><br><span class="line">req.on(<span class="string">"end"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> buffer = Buffer.concat(arr); <span class="comment">//合并接收到的数据</span></span><br><span class="line">	<span class="keyword">let</span> result = querystring.parse(buffer);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><h4 id="模块化"><a href="#模块化" class="headerlink" title="模块化"></a>模块化</h4><h4 id="module-批量导出"><a href="#module-批量导出" class="headerlink" title="module 批量导出"></a><code>module</code> 批量导出</h4><h4 id="exports-导出"><a href="#exports-导出" class="headerlink" title="exports 导出"></a><code>exports</code> 导出</h4><h4 id="require-引入"><a href="#require-引入" class="headerlink" title="require 引入"></a><code>require</code> 引入</h4><ul><li>如果带有路径——去路径下面找</li><li>如果没有：<ul><li>node_modules文件夹</li><li>系统node_modules</li></ul></li></ul><h3 id="第三方模块"><a href="#第三方模块" class="headerlink" title="第三方模块"></a>第三方模块</h3><h4 id="multiparty-上传文件处理"><a href="#multiparty-上传文件处理" class="headerlink" title="multiparty 上传文件处理"></a><code>multiparty</code> 上传文件处理</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">"http"</span>)</span><br><span class="line"><span class="keyword">const</span> multiparty = <span class="built_in">require</span>(<span class="string">"multiparty"</span>);</span><br><span class="line"></span><br><span class="line">http.createServer(<span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">let</span> form = <span class="keyword">new</span> multiparty.Form(&#123;</span><br><span class="line">		<span class="comment">// 上传后的目录</span></span><br><span class="line">		uploadDir: <span class="string">'./www'</span></span><br><span class="line">	&#125;);</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 解析</span></span><br><span class="line">	form.parse(req);</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 处理正常信息</span></span><br><span class="line">	form.on(<span class="function">(<span class="params"><span class="string">'filed'</span>, (name, value</span>) =&gt;</span>&#123;</span><br><span class="line">		<span class="built_in">console</span>.log(name, value);</span><br><span class="line">	&#125;));</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 处理文件信息</span></span><br><span class="line">	form.on(<span class="string">'file'</span>, (name, file) =&gt; &#123;</span><br><span class="line">		<span class="built_in">console</span>.log(name, file);</span><br><span class="line">	&#125;);</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 解析结束后执行</span></span><br><span class="line">	form.on(<span class="string">'close'</span>, () =&gt; &#123;</span><br><span class="line">		<span class="built_in">console</span>.log(<span class="string">'处理完成'</span>);</span><br><span class="line">	&#125;)</span><br><span class="line"></span><br><span class="line">&#125;).listen(<span class="number">8080</span>);</span><br></pre></td></tr></table></figure><h4 id="socket-io"><a href="#socket-io" class="headerlink" title="socket.io"></a><code>socket.io</code></h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http=<span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> io=<span class="built_in">require</span>(<span class="string">'socket.io'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//1.建立普通http</span></span><br><span class="line"><span class="keyword">let</span> server=http.createServer(<span class="function">(<span class="params">req, res</span>)=&gt;</span>&#123;&#125;);</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//2.建立ws</span></span><br><span class="line"><span class="keyword">let</span> wsServer=io.listen(server);</span><br><span class="line">wsServer.on(<span class="string">'connection'</span>, sock=&gt;&#123;</span><br><span class="line">  sock.emit(<span class="string">'name'</span>, 数据)  <span class="comment">//发布</span></span><br><span class="line">  sock.on(<span class="string">'name'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">数据</span>)</span>&#123;&#125;);  <span class="comment">//接收</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><h3 id="数据交互"><a href="#数据交互" class="headerlink" title="数据交互"></a>数据交互</h3><h4 id="ajax-原生"><a href="#ajax-原生" class="headerlink" title="ajax  原生"></a><code>ajax</code> 原生</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> xhr = <span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line">xhr.open(<span class="string">'GET'</span>, <span class="string">'http://localhost/a'</span>, <span class="literal">true</span>);</span><br><span class="line">xhr.send();</span><br><span class="line"></span><br><span class="line">xhr.onreadystatechenge = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (xhr.readyState == <span class="number">4</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span> (xhr.status == <span class="number">200</span> &amp;&amp; xhr.status == <span class="number">304</span>)&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'成功'</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">'失败'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><ul><li>同源策略</li><li>跨域处理<ul><li>主要由后台处理</li><li><code>origin</code>和<code>access-control-allow-origin</code>决定</li></ul></li></ul><h4 id="fetch-另一种ajax的封装"><a href="#fetch-另一种ajax的封装" class="headerlink" title="fetch 另一种ajax的封装"></a><code>fetch</code> 另一种ajax的封装</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> oBtn=<span class="built_in">document</span>.getElementById(<span class="string">'btn1'</span>);</span><br><span class="line">oBtn.onclick=<span class="keyword">async</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="comment">//1.请求</span></span><br><span class="line">    <span class="keyword">let</span> res=<span class="keyword">await</span> fetch(<span class="string">'data/1.txt'</span>);</span><br><span class="line">    <span class="comment">//2.解析</span></span><br><span class="line">    <span class="keyword">let</span> str=<span class="keyword">await</span> res.text(); <span class="comment">//解析文本</span></span><br><span class="line">    <span class="keyword">let</span> str = <span class="keyword">await</span> res.json(); <span class="comment">//解析json对象</span></span><br><span class="line">    alert(str);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">let</span> str = <span class="keyword">await</span> res.blob(); <span class="comment">//解析二进制文件</span></span><br><span class="line">	<span class="keyword">let</span> url = URL.createObjectURL(str); </span><br><span class="line">	oImg.src = url;   </span><br><span class="line"></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure><h4 id="jsonp"><a href="#jsonp" class="headerlink" title="jsonp"></a><code>jsonp</code></h4><ul><li>直接使用<code>script</code>标签</li><li>使用jQuery</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    $(<span class="string">'#txt1'</span>).on(<span class="string">'input'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">        $.ajax(&#123;</span><br><span class="line">            url: <span class="string">'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su'</span>,</span><br><span class="line">            <span class="comment">// https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=xx&amp;cb=xxx</span></span><br><span class="line">            data: &#123;<span class="attr">wd</span>: $(<span class="keyword">this</span>).val()&#125;,</span><br><span class="line">            dataType: <span class="string">'jsonp'</span>,</span><br><span class="line">            jsonp: <span class="string">'cb'</span></span><br><span class="line">        &#125;).then(<span class="function">(<span class="params">&#123;s&#125;</span>)=&gt;</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(s);</span><br><span class="line">        &#125;, res=&gt;&#123;</span><br><span class="line">            alert(<span class="string">'失败'</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure><h4 id="Ajax2-0-formdata"><a href="#Ajax2-0-formdata" class="headerlink" title="Ajax2.0(formdata)"></a>Ajax2.0(<code>formdata</code>)</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 原生</span></span><br><span class="line"><span class="keyword">let</span> oForm=<span class="built_in">document</span>.querySelector(<span class="string">'#form1'</span>);</span><br><span class="line"></span><br><span class="line">oForm.onsubmit=<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">let</span> formdata=<span class="keyword">new</span> FormData(oForm);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> xhr=<span class="keyword">new</span> XMLHttpRequest();</span><br><span class="line"></span><br><span class="line">    xhr.open(oForm.method, oForm.action, <span class="literal">true</span>);</span><br><span class="line">    xhr.send(formdata);</span><br><span class="line"></span><br><span class="line">    xhr.onreadystatechange=<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(xhr.readyState==<span class="number">4</span>)&#123;</span><br><span class="line">            <span class="keyword">if</span>(xhr.status==<span class="number">200</span>)&#123;</span><br><span class="line">                alert(<span class="string">'成功'</span>);</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                alert(<span class="string">'失败'</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">    </span><br><span class="line"><span class="comment">//jQuery</span></span><br><span class="line">    $(<span class="string">'#form1'</span>).on(<span class="string">'submit'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">let</span> formdata=<span class="keyword">new</span> FormData(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        $.ajax(&#123;</span><br><span class="line">            url: <span class="keyword">this</span>.action,</span><br><span class="line">            type: <span class="keyword">this</span>.method,</span><br><span class="line">            data: formdata,</span><br><span class="line">            </span><br><span class="line">            <span class="comment">// 阻止ajax自动处理数据</span></span><br><span class="line">            processData: <span class="literal">false</span>,</span><br><span class="line">            contentType: <span class="literal">false</span></span><br><span class="line">            </span><br><span class="line">        &#125;).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">            alert(<span class="string">'成功'</span>);</span><br><span class="line">        &#125;, res=&gt;&#123;</span><br><span class="line">            alert(<span class="string">'失败'</span>);</span><br><span class="line">        &#125;);</span><br></pre></td></tr></table></figure><h4 id="webSocket"><a href="#webSocket" class="headerlink" title="webSocket"></a><code>webSocket</code></h4><p><code>socket.io</code></p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 服务器</span></span><br><span class="line"><span class="keyword">const</span> http=<span class="built_in">require</span>(<span class="string">'http'</span>);</span><br><span class="line"><span class="keyword">const</span> io=<span class="built_in">require</span>(<span class="string">'socket.io'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//1.建立普通http</span></span><br><span class="line"><span class="keyword">let</span> server=http.createServer(<span class="function">(<span class="params">req, res</span>)=&gt;</span>&#123;&#125;);</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//2.建立ws</span></span><br><span class="line"><span class="keyword">let</span> wsServer=io.listen(server);</span><br><span class="line">wsServer.on(<span class="string">'connection'</span>, sock=&gt;&#123;</span><br><span class="line">  <span class="comment">//sock.emit('name', 数据)</span></span><br><span class="line">  <span class="comment">//sock.on('name', function (数据)&#123;&#125;);</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">/*sock.on('aaa', function (a, b)&#123;</span></span><br><span class="line"><span class="comment">    console.log(a, b, a+b);</span></span><br><span class="line"><span class="comment">  &#125;);*/</span></span><br><span class="line"></span><br><span class="line">  setInterval(<span class="function"><span class="keyword">function</span> (<span class="params"></span>)</span>&#123;</span><br><span class="line">    sock.emit(<span class="string">'timer'</span>, <span class="keyword">new</span> <span class="built_in">Date</span>().getTime());</span><br><span class="line">  &#125;, <span class="number">1000</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//客户端</span></span><br><span class="line"><span class="keyword">let</span> sock=io.connect(<span class="string">'ws://localhost:8080/'</span>); <span class="comment">//建立连接</span></span><br><span class="line">sock.emit()  <span class="comment">//发布</span></span><br><span class="line">sock.on(名称, ()=&gt;&#123;&#125;)  <span class="comment">//接收</span></span><br></pre></td></tr></table></figure><h3 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h3><h4 id="mysql包和co-mysql包"><a href="#mysql包和co-mysql包" class="headerlink" title="mysql包和co-mysql包"></a><code>mysql</code>包和<code>co-mysql</code>包</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 初始链接</span></span><br><span class="line"><span class="keyword">let</span> conn = mysql.createConnection(&#123;</span><br><span class="line">    host: <span class="string">'localhost'</span>,</span><br><span class="line">    port: <span class="number">3306</span>,</span><br><span class="line">    user: <span class="string">'root'</span>,</span><br><span class="line">    password: <span class="string">''</span>,</span><br><span class="line">    database: <span class="string">'20192'</span>,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建连接池</span></span><br><span class="line"><span class="keyword">let</span> conn = mysql.createPool(&#123;</span><br><span class="line">    host: <span class="string">'localhost'</span>,</span><br><span class="line">    port: <span class="number">3306</span>,</span><br><span class="line">    user: <span class="string">'root'</span>,</span><br><span class="line">    password: <span class="string">''</span>,</span><br><span class="line">    database: <span class="string">'20192'</span>,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">//使用co-mysql (可以使用async/await)</span></span><br><span class="line"><span class="keyword">let</span> db = co(conn);</span><br><span class="line"></span><br><span class="line"><span class="comment">//执行SQL语句</span></span><br><span class="line">db.query(<span class="string">`SQL语句`</span>,(err, data)=&gt;&#123;&#125;)</span><br></pre></td></tr></table></figure><h3 id="流操作"><a href="#流操作" class="headerlink" title="流操作"></a>流操作</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> fs = <span class="built_in">require</span>(<span class="string">'fs'</span>);</span><br><span class="line"><span class="comment">// 压缩包</span></span><br><span class="line"><span class="keyword">let</span> zlib = <span class="built_in">require</span>(<span class="string">'zlib'</span>); </span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建流</span></span><br><span class="line"><span class="keyword">let</span> rs = fs.createReadStream(<span class="string">'1.txt'</span>);</span><br><span class="line"><span class="keyword">let</span> gz = zlib.createGzip(); <span class="comment">//创建压缩流</span></span><br><span class="line"><span class="keyword">let</span> ws = fs.createWriteStream(<span class="string">'2.gz'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 对接</span></span><br><span class="line">rs.pipe(ws);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 压缩以后再写入 (读写流)</span></span><br><span class="line">rs.pipe(gz).pipe(ws); </span><br><span class="line"></span><br><span class="line"><span class="comment">//错误事件</span></span><br><span class="line">rs.on(<span class="string">'error'</span>, err =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(err);</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 完成事件</span></span><br><span class="line">fs.on(<span class="string">'finish'</span>, (err, data) =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span>(err)&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(err);</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'成功'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><p>注：http中的req, res本身就是流,所以可以直接进行流操作</p><h3 id="启动器"><a href="#启动器" class="headerlink" title="启动器"></a>启动器</h3><p><code>forever</code></p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装</span></span><br><span class="line">npm i forever -g</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用</span></span><br><span class="line">forever start xxx.js</span><br><span class="line">forever restart xxx.js</span><br><span class="line">forever stop xxx.js</span><br><span class="line">forever stopall</span><br></pre></td></tr></table></figure><h3 id="框架"><a href="#框架" class="headerlink" title="框架"></a>框架</h3><h4 id="express"><a href="#express" class="headerlink" title="express"></a><code>express</code></h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装</span></span><br><span class="line">npm install express --save-dev</span><br></pre></td></tr></table></figure><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用</span></span><br><span class="line"><span class="keyword">let</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>)</span><br><span class="line"><span class="keyword">let</span> server = express();</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法</span></span><br><span class="line">server.get(<span class="function">(<span class="params">req, res, next</span>) =&gt;</span> &#123;&#125;);  <span class="comment">//处理get请求 </span></span><br><span class="line">server.post(<span class="function">(<span class="params">req, res, next</span>) =&gt;</span> &#123;&#125;); <span class="comment">//处理post请求</span></span><br><span class="line">server.use(中间件)   </span><br><span class="line"></span><br><span class="line"><span class="comment">//next()调用中间件</span></span><br></pre></td></tr></table></figure><h4 id="中间件"><a href="#中间件" class="headerlink" title="中间件"></a>中间件</h4><p>express的中间建文档汇总:<a href="http://expressjs.com/en/resources/middleware.html" target="_blank" rel="noopener">http://expressjs.com/en/resources/middleware.html</a></p><ul><li><code>body-parser</code> 在处理程序之前在中间件中解析传入的请求主体，该处理程序在<code>req.body</code>属性下可用。</li><li><code>multi</code> 处理文件上传。</li><li><code>cookie-parser</code> 解析和设置cookie</li><li><code>cookie-session</code> 解析和设置session</li></ul><h4 id="KOA"><a href="#KOA" class="headerlink" title="KOA"></a>KOA</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装</span></span><br><span class="line">npm install koa -D</span><br><span class="line"><span class="comment"># 安装路由中间件</span></span><br><span class="line">npm install koa-router -D</span><br></pre></td></tr></table></figure><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Koa = <span class="built_in">require</span>(<span class="string">'koa'</span>);</span><br><span class="line"><span class="keyword">const</span> Router = <span class="built_in">require</span>(<span class="string">'koa-router'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建服务器</span></span><br><span class="line"><span class="keyword">let</span> server = <span class="keyword">new</span> Koa();</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	server.context 相当于 ctx的prototype,也就是在这里设置的属性,可以在下面调用.</span></span><br><span class="line"><span class="comment">	server.context.a = 12;</span></span><br><span class="line"><span class="comment">	直接在路由中调用:</span></span><br><span class="line"><span class="comment">	console.log(ctx.a)  //12</span></span><br><span class="line"><span class="comment">*/</span> </span><br><span class="line"></span><br><span class="line"><span class="comment">//错误处理</span></span><br><span class="line">server.use(<span class="keyword">async</span> (ctx, next) =&gt;&#123;</span><br><span class="line">    <span class="keyword">try</span>&#123;</span><br><span class="line">       <span class="keyword">await</span> next();</span><br><span class="line">    &#125;<span class="keyword">catch</span>(e)&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(e);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 创建路由</span></span><br><span class="line"><span class="keyword">let</span> router = <span class="keyword">new</span> Router();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 设置路由,ctx表示上下文, next表示路由相同时同时调用</span></span><br><span class="line">router.get(<span class="string">'/'</span>, <span class="keyword">async</span> (ctx, next) =&gt; &#123;</span><br><span class="line">    ctx.body = <span class="string">'aaa'</span>; </span><br><span class="line">	<span class="comment">// ctx的方法 </span></span><br><span class="line">    <span class="comment">// 信息:</span></span><br><span class="line">    ctx.method  <span class="comment">// 请求方法</span></span><br><span class="line">    ctx.url     <span class="comment">// 请求的地址</span></span><br><span class="line">    ctx.path    <span class="comment">// 请求的路径</span></span><br><span class="line">    ctx.query   <span class="comment">// get数据</span></span><br><span class="line">    ctx.params  <span class="comment">// 路径id</span></span><br><span class="line">    ctx.ip      <span class="comment">// 客户端IP</span></span><br><span class="line">    ctx.headers <span class="comment">// 请求头</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 返回信息</span></span><br><span class="line">    ctx.throw(状态码, msg) <span class="comment">//返回信息并退出</span></span><br><span class="line">    ctx.assert(条件, 状态码, msg)  <span class="comment">//断点调试</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 函数</span></span><br><span class="line">    ctx.state = <span class="number">200</span> <span class="comment">//设置状态码</span></span><br><span class="line">    ctx.redirect(路径) <span class="comment">//重定向</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// await next();</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// /user/3121</span></span><br><span class="line">router.get(<span class="string">'/user:id'</span>, ctx =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;id&#125; = ctx.params;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// /user?a=1&amp;b=12</span></span><br><span class="line">router.get(<span class="string">'/user/'</span>, ctx =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> &#123;a, b&#125; = ctx.query;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// 使用路由</span></span><br><span class="line">server.use(router.routes());</span><br><span class="line"></span><br><span class="line"><span class="comment">//cookie</span></span><br><span class="line">server.keys = [</span><br><span class="line">    <span class="string">'dadwadawdwad'</span>,</span><br><span class="line">    <span class="string">'dwadwadwads'</span>,</span><br><span class="line">    <span class="string">'dwadwadasasdw'</span></span><br><span class="line">]</span><br><span class="line">server.use(<span class="keyword">async</span> ctx =&gt;&#123;</span><br><span class="line">    <span class="comment">// 设置cookie</span></span><br><span class="line">    ctx.cookies.set(name, value, &#123;</span><br><span class="line">        signed: <span class="literal">true</span>, <span class="comment">//是否设置签名</span></span><br><span class="line">        maxAge: <span class="number">12</span>*<span class="number">60</span>*<span class="number">1000</span>, <span class="comment">//设置有效期</span></span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">// 获取cookie</span></span><br><span class="line">    ctx.cookies.get(name, &#123;</span><br><span class="line">        signed: <span class="literal">true</span>, <span class="comment">//是否验证签名</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h4 id="中间件-1"><a href="#中间件-1" class="headerlink" title="中间件"></a>中间件</h4><h6 id="koa-router路由"><a href="#koa-router路由" class="headerlink" title="koa-router路由"></a><code>koa-router</code>路由</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">router.get(); <span class="comment">//get方式</span></span><br><span class="line">router.post(); <span class="comment">//post方式</span></span><br><span class="line">router.use();  <span class="comment">//所有请求</span></span><br></pre></td></tr></table></figure><div class="table-container"><table><thead><tr><th style="text-align:center">urlencoded(?a=1&amp;b=12)</th><th style="text-align:center">params(/:id)</th></tr></thead><tbody><tr><td style="text-align:center">顺序灵活</td><td style="text-align:center">不灵活</td></tr><tr><td style="text-align:center">可省略</td><td style="text-align:center">不可省略</td></tr><tr><td style="text-align:center">不利于SEO</td><td style="text-align:center">利于SEO</td></tr></tbody></table></div><h6 id="路由嵌套"><a href="#路由嵌套" class="headerlink" title="路由嵌套"></a>路由嵌套</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// admin.js </span></span><br><span class="line"><span class="keyword">const</span> Router=<span class="built_in">require</span>(<span class="string">'koa-router'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> router=<span class="keyword">new</span> Router();</span><br><span class="line"></span><br><span class="line">router.get(<span class="string">'/a'</span>, <span class="keyword">async</span> ctx=&gt;&#123;</span><br><span class="line">  ctx.body=<span class="string">'管理员的a'</span>;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports=router.routes();</span><br><span class="line"></span><br><span class="line"><span class="comment">// index.js</span></span><br><span class="line"><span class="keyword">const</span> Router=<span class="built_in">require</span>(<span class="string">'koa-router'</span>);</span><br><span class="line"><span class="keyword">const</span> Admin = <span class="built_in">require</span>(<span class="string">'./admin'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> router=<span class="keyword">new</span> Router();</span><br><span class="line"></span><br><span class="line">router.get(<span class="string">'/'</span>, <span class="keyword">async</span> ctx=&gt;&#123;</span><br><span class="line">  ctx.body=<span class="string">'用户'</span>;</span><br><span class="line">&#125;);</span><br><span class="line">router.use(<span class="string">'/admin'</span>, Admin);</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports=router.routes();</span><br></pre></td></tr></table></figure><h6 id="koa-static-处理所有静态文件"><a href="#koa-static-处理所有静态文件" class="headerlink" title="koa-static 处理所有静态文件"></a><code>koa-static</code> 处理所有静态文件</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="keyword">static</span> = <span class="built_in">require</span>(<span class="string">'koa-static'</span>)</span><br><span class="line">server.use(<span class="keyword">static</span>(路径, &#123;</span><br><span class="line">    maxage: <span class="number">60</span>*<span class="number">86400</span>*<span class="number">1000</span>; <span class="comment">//有效期</span></span><br><span class="line">    index: <span class="string">'1.html'</span> <span class="comment">//默认页面</span></span><br><span class="line">&#125;))</span><br></pre></td></tr></table></figure><h6 id="koa-better-body-数据处理"><a href="#koa-better-body-数据处理" class="headerlink" title="koa-better-body  数据处理"></a><code>koa-better-body</code> 数据处理</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> body = <span class="built_in">require</span>(<span class="string">'koa-better-body'</span>)</span><br><span class="line">server.use(<span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">    <span class="comment">//对文件和post数据处理</span></span><br><span class="line">    <span class="built_in">console</span>.log(ctx.request.fields);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h6 id="koa-session"><a href="#koa-session" class="headerlink" title="koa-session"></a><code>koa-session</code></h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> session = <span class="built_in">require</span>(<span class="string">'koa-session'</span>)</span><br><span class="line">server.keys = [</span><br><span class="line">    <span class="string">'dasdawdadswad'</span>,</span><br><span class="line">    <span class="string">'dwadsadwda'</span></span><br><span class="line">];</span><br><span class="line">server.use(session(&#123;</span><br><span class="line">    maxAge: <span class="number">12</span>*<span class="number">60</span>*<span class="number">1000</span>, <span class="comment">//有效期</span></span><br><span class="line">    renew: <span class="literal">true</span>,        <span class="comment">//自动续约</span></span><br><span class="line">&#125;, server));</span><br></pre></td></tr></table></figure><h6 id="koa-ejs-服务器渲染"><a href="#koa-ejs-服务器渲染" class="headerlink" title="koa-ejs 服务器渲染"></a><code>koa-ejs</code> 服务器渲染</h6><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Koa = <span class="built_in">require</span>(<span class="string">'koa'</span>)</span><br><span class="line"><span class="keyword">const</span> ejs = <span class="built_in">require</span>(<span class="string">'koa-ejs'</span>)</span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> server = <span class="keyword">new</span> Koa();</span><br><span class="line">server.listen(<span class="number">8080</span>);</span><br><span class="line"></span><br><span class="line">ejs(server, &#123;</span><br><span class="line">    layout: <span class="literal">false</span>,   </span><br><span class="line">    root: <span class="string">'template'</span>, <span class="comment">// 模板路径</span></span><br><span class="line">    viewExt: <span class="string">'ejs'</span>,   <span class="comment">// 使用模板文件的后缀名</span></span><br><span class="line">    cache: <span class="literal">false</span>,     <span class="comment">// 是否缓存</span></span><br><span class="line">    debug: <span class="literal">false</span>      <span class="comment">// 是否输出信息</span></span><br><span class="line">&#125;);</span><br><span class="line">server.use(<span class="keyword">async</span> ctx =&gt; &#123;</span><br><span class="line">    <span class="keyword">await</span> ctx.render(模板文件名,&#123;</span><br><span class="line">        arr: [<span class="number">2</span>,<span class="number">3</span>,<span class="number">1</span>,<span class="number">4</span>]</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="服务端渲染-客户端渲染"><a href="#服务端渲染-客户端渲染" class="headerlink" title="服务端渲染/客户端渲染"></a>服务端渲染/客户端渲染</h3><ul><li>服务端渲染：服务器将页面渲染完成以后直接发给客户端，主要的渲染引擎有pug、ejs</li><li>客户端渲染：服务器只是发送一些数据给客户端，页面渲染工作由客户端负责，主要框架有vue，react</li></ul><h4 id="pug"><a href="#pug" class="headerlink" title="pug"></a>pug</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install pug -D</span><br></pre></td></tr></table></figure><ul><li>通过缩进来区分标签</li><li>在标签后直接写内容则表示这个标签的内容</li><li>在标签后加<code>=</code>表示接受后台传输的值</li><li>标签后的<code>.</code>表示下面所有内容是和这个标签一起的</li><li>标签的属性通过括号<code>（）</code>来添加</li></ul><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">html</span><br><span class="line">  head</span><br><span class="line">    meta(charset='utf-8')</span><br><span class="line">    title test</span><br><span class="line">  body</span><br><span class="line">    script.</span><br><span class="line">      let a = 1;</span><br><span class="line">      let b= 2;</span><br></pre></td></tr></table></figure><h4 id="ejs"><a href="#ejs" class="headerlink" title="ejs"></a>ejs</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i ejs -D</span><br></pre></td></tr></table></figure><ul><li>用<code>&lt;% %&gt;</code>把需要插入的内容隔开就行</li></ul><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="meta-keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span> <span class="attr">dir</span>=<span class="string">"ltr"</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"utf-8"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span><span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    &lt;%for(let i=0;i&lt;arr.length;i++)&#123;%&gt;</span><br><span class="line">      <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">""</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">%=arr[i]%</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">%&#125;%</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong> 墨轩啊</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="https://moxuan.xyz/2019/11/21/nodejs/" title="nodejs笔记">https://moxuan.xyz/2019/11/21/nodejs/</a></li><li class="post-copyright-license"><strong>版权声明：</strong> 本博客所有文章除特别声明外，均采用<a href="https://creativecommons.org/licenses/by-nc-sa/4.0/null" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i> BY-NC-SA</a> 许可协议。转载请注明出处！</li></ul></div><footer class="post-footer"><div class="post-tags"><a href="/tags/%E8%AE%B0%E5%BD%95/" rel="tag"><i class="fa fa-tag"></i> 记录</a><a href="/tags/Node/" rel="tag"><i class="fa fa-tag"></i> Node</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/2019/11/14/ES6%E8%AF%AD%E6%B3%95%E7%AC%94%E8%AE%B0%E6%80%BB%E7%BB%93/" rel="next" title="ES6语法笔记总结"><i class="fa fa-chevron-left"></i> ES6语法笔记总结</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/2019/11/22/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E7%AC%94%E8%AE%B0/" rel="prev" title="版本控制笔记">版本控制笔记<i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div><div class="comments" id="gitalk-container"></div></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span><span class="toggle-line toggle-line-middle"></span><span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width="300" height="86" src="//music.163.com/outchain/player?type=2&id=1350160463&auto=0&height=66"></iframe><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#用途"><span class="nav-number">1.</span> <span class="nav-text">用途</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#优势"><span class="nav-number">2.</span> <span class="nav-text">优势</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#卸载"><span class="nav-number">3.</span> <span class="nav-text">卸载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#系统自带模块"><span class="nav-number">4.</span> <span class="nav-text">系统自带模块</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#http"><span class="nav-number">4.1.</span> <span class="nav-text">http</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#fs"><span class="nav-number">4.2.</span> <span class="nav-text">fs</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#querystring"><span class="nav-number">4.3.</span> <span class="nav-text">querystring</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#url"><span class="nav-number">4.4.</span> <span class="nav-text">url</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#assert-断言"><span class="nav-number">4.5.</span> <span class="nav-text">assert 断言</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#path"><span class="nav-number">4.6.</span> <span class="nav-text">path</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#服务器相关内容"><span class="nav-number">5.</span> <span class="nav-text">服务器相关内容</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#http协议"><span class="nav-number">5.1.</span> <span class="nav-text">http协议</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#响应码"><span class="nav-number">5.2.</span> <span class="nav-text">响应码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#请求方式"><span class="nav-number">5.3.</span> <span class="nav-text">请求方式</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#GET处理"><span class="nav-number">5.3.0.1.</span> <span class="nav-text">GET处理</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#POST处理"><span class="nav-number">5.3.0.2.</span> <span class="nav-text">POST处理</span></a></li></ol></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#模块化"><span class="nav-number">5.4.</span> <span class="nav-text">模块化</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#module-批量导出"><span class="nav-number">5.5.</span> <span class="nav-text">module 批量导出</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#exports-导出"><span class="nav-number">5.6.</span> <span class="nav-text">exports 导出</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#require-引入"><span class="nav-number">5.7.</span> <span class="nav-text">require 引入</span></a></li></ol><li class="nav-item nav-level-3"><a class="nav-link" href="#第三方模块"><span class="nav-number">6.</span> <span class="nav-text">第三方模块</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#multiparty-上传文件处理"><span class="nav-number">6.1.</span> <span class="nav-text">multiparty 上传文件处理</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#socket-io"><span class="nav-number">6.2.</span> <span class="nav-text">socket.io</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据交互"><span class="nav-number">7.</span> <span class="nav-text">数据交互</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#ajax-原生"><span class="nav-number">7.1.</span> <span class="nav-text">ajax 原生</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#fetch-另一种ajax的封装"><span class="nav-number">7.2.</span> <span class="nav-text">fetch 另一种ajax的封装</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#jsonp"><span class="nav-number">7.3.</span> <span class="nav-text">jsonp</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Ajax2-0-formdata"><span class="nav-number">7.4.</span> <span class="nav-text">Ajax2.0(formdata)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#webSocket"><span class="nav-number">7.5.</span> <span class="nav-text">webSocket</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据库"><span class="nav-number">8.</span> <span class="nav-text">数据库</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#mysql包和co-mysql包"><span class="nav-number">8.1.</span> <span class="nav-text">mysql包和co-mysql包</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#流操作"><span class="nav-number">9.</span> <span class="nav-text">流操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#启动器"><span class="nav-number">10.</span> <span class="nav-text">启动器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#框架"><span class="nav-number">11.</span> <span class="nav-text">框架</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#express"><span class="nav-number">11.1.</span> <span class="nav-text">express</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#中间件"><span class="nav-number">11.2.</span> <span class="nav-text">中间件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#KOA"><span class="nav-number">11.3.</span> <span class="nav-text">KOA</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#中间件-1"><span class="nav-number">11.4.</span> <span class="nav-text">中间件</span></a><ol class="nav-child"><li class="nav-item nav-level-6"><a class="nav-link" href="#koa-router路由"><span class="nav-number">11.4.0.1.</span> <span class="nav-text">koa-router路由</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#路由嵌套"><span class="nav-number">11.4.0.2.</span> <span class="nav-text">路由嵌套</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#koa-static-处理所有静态文件"><span class="nav-number">11.4.0.3.</span> <span class="nav-text">koa-static 处理所有静态文件</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#koa-better-body-数据处理"><span class="nav-number">11.4.0.4.</span> <span class="nav-text">koa-better-body 数据处理</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#koa-session"><span class="nav-number">11.4.0.5.</span> <span class="nav-text">koa-session</span></a></li><li class="nav-item nav-level-6"><a class="nav-link" href="#koa-ejs-服务器渲染"><span class="nav-number">11.4.0.6.</span> <span class="nav-text">koa-ejs 服务器渲染</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#服务端渲染-客户端渲染"><span class="nav-number">12.</span> <span class="nav-text">服务端渲染&#x2F;客户端渲染</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#pug"><span class="nav-number">12.1.</span> <span class="nav-text">pug</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#ejs"><span class="nav-number">12.2.</span> <span class="nav-text">ejs</span></a></li></ol></li></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="墨轩啊" src="/images/tlogo.png"><p class="site-author-name" itemprop="name">墨轩啊</p><div class="site-description" itemprop="description">专注前端技术，包括vue,react，涉猎的主要编程语言是Python,JavaScript，Node，朝着全栈方向努力！。</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">35</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/categories/"><span class="site-state-item-count">50</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">29</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="feed-link motion-element"><a href="/atom.xml" rel="alternate"><i class="fa fa-rss"></i> RSS</a></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/moxuan-jst" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;moxuan-jst" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i> GitHub</a></span><span class="links-of-author-item"><a href="mailto:moxuanjst@foxmail.com" title="E-Mail &amp;rarr; mailto:moxuanjst@foxmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i> E-Mail</a></span><span class="links-of-author-item"><a href="https://weibo.com/2690290777/profile?topnav=1&wvr=6&is_all=1" title="Weibo &amp;rarr; https:&#x2F;&#x2F;weibo.com&#x2F;2690290777&#x2F;profile?topnav&#x3D;1&amp;wvr&#x3D;6&amp;is_all&#x3D;1" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i> Weibo</a></span></div><div class="cc-license motion-element" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/null" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a></div></div><div class="back-to-top motion-element"><i class="fa fa-arrow-up"></i> <span>0%</span></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><link rel="stylesheet" href="/live2d/css/live2d.css"><div id="landlord"><div class="message" style="opacity:0"></div><canvas id="live2d" width="280" height="250" class="live2d"></canvas><div class="hide-button">隐藏</div></div><script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script><script>var message_Path="/live2d/",home_Path="https://calmcenter.club/"</script><script src="/live2d/js/live2d.js"></script><script src="/live2d/js/message.js"></script><script>loadlive2d("live2d","/live2d/model/tia/model.json")</script><script>loadlive2d("live2d","/live2d/model/xuexiaoban/model.json")</script><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span><span class="with-love"><i class="fa fa-user"></i></span> <span class="author" itemprop="copyrightHolder">by 墨轩啊</span><div><i class="fa fa-user-md"></i> <span id="busuanzi_container_site_uv">本站访客数:<span id="busuanzi_value_site_pv"></span></span></div><span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span><script>var now=new Date;function createtime(){var n=new Date("10/31/2019 12:00:00");now.setTime(now.getTime()+250),days=(now-n)/1e3/60/60/24,dnum=Math.floor(days),hours=(now-n)/1e3/60/60-24*dnum,hnum=Math.floor(hours),1==String(hnum).length&&(hnum="0"+hnum),minutes=(now-n)/1e3/60-1440*dnum-60*hnum,mnum=Math.floor(minutes),1==String(mnum).length&&(mnum="0"+mnum),seconds=(now-n)/1e3-86400*dnum-3600*hnum-60*mnum,snum=Math.round(seconds),1==String(snum).length&&(snum="0"+snum),document.getElementById("timeDate").innerHTML="本站已安全运行 "+dnum+" 天 ",document.getElementById("times").innerHTML=hnum+" 小时 "+mnum+" 分 "+snum+" 秒"}setInterval("createtime()",250)</script></div><div class="busuanzi-count"><script pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i></span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span></span></span></div></div></footer></div><script color="113, 113, 113" opacity="0.5" zindex="-1" count="99" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1/canvas-nest.min.js"></script><script src="/lib/anime.min.js"></script><script src="/lib/pjax/pjax.min.js"></script><script src="/lib/velocity/velocity.min.js"></script><script src="/lib/velocity/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/pisces.js"></script><script src="/js/next-boot.js"></script><script>var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.getAttribute('pjax') !== null) {
      element.setAttribute('pjax', '');
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});</script><script pjax>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/js/local-search.js"></script><div id="pjax"><link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css"><script>NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID: '131702b03dc5fc7d59ec',
      clientSecret: 'b2bc14c3066a1607a4d218639b7a49eb71365f84',
      repo: 'moxuan-jst.github.io',
      owner: 'moxuan-jst',
      admin: ['moxuan-jst'],
      id: 'd0a40cd7d5ce84a7663072e9cd52807b',
        language: 'zh-CN',
      distractionFreeMode: 'true'
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);</script></div><script src="/js/clicklove.js"></script></body></html>